/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.marlboroug.admins;

import com.marlboroug.entity.Admins;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;


@Stateless
public class AdminsManager implements AdminsManagerRemote {
    
    @PersistenceContext(unitName = "Marlboroug-ejbPU")
    EntityManager em;
    
    /** Permet de renvoyer un objet Admin en fonction d'un ID
     * 
     * @param ID_admins Un ID
     * @return Un objet de type Admins
     */
    @Override
    public Admins find (Long ID_admins) {
        Admins admins = em.find(Admins.class, ID_admins);
        return admins;
    }
    
    /** Permet de mettre à jour un Admin
     * 
     * @param admins Un objet Admins
     */
    @Override
    public void update (Admins admins) {
        em.merge(admins);
    }
    
    /** Pour vérifier le pseudo d'un admin
     * 
     * @param pattern Envoie le pseudo à tester
     * @return Un objet de type Admins
     */
    @Override
    public List<Admins> AuthentificationPseudo (String pattern) {       
        String q = "SELECT m FROM Admins m WHERE m.pseudo LIKE :pattern";
        TypedQuery<Admins> query = em.createQuery(q, Admins.class);
        query.setParameter("pattern", pattern);
        List<Admins> admins = query.getResultList();
        return admins;
    }
    
    /** Pour vérifier le MDP d'un admin
     * 
     * @param pattern Envoie le MDP à tester
     * @return Un objet de type Admins
     */
    @Override
    public List<Admins> AuthentificationPassword (String pattern) {       
        String q = "SELECT m FROM Admins m WHERE m.password LIKE :pattern";
        TypedQuery<Admins> query = em.createQuery(q, Admins.class);
        query.setParameter("pattern", pattern);
        List<Admins> admins = query.getResultList();
        return admins;
    }
    
}
